
<!doctype html>
<html lang="zh" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      <link rel="icon" href="assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.3.1, mkdocs-material-8.4.0">
    
    
      
        <title>GMLib 文档</title>
      
    
    
      <link rel="stylesheet" href="assets/stylesheets/main.69437709.min.css">
      
        
        <link rel="stylesheet" href="assets/stylesheets/palette.cbb835fc.min.css">
        
      
      
    
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
    <script>__md_scope=new URL(".",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
  </head>
  
  
    
    
    
    
    
    <body dir="ltr" data-md-color-scheme="" data-md-color-primary="none" data-md-color-accent="none">
  
    
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#gmlib" class="md-skip">
          跳转至
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

<header class="md-header" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="页眉">
    <a href="." title="GMLib 文档" class="md-header__button md-logo" aria-label="GMLib 文档" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            GMLib 文档
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              快速开始
            
          </span>
        </div>
      </div>
    </div>
    
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
      </label>
      <div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="搜索" placeholder="搜索" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="查找">
        
        <button type="reset" class="md-search__icon md-icon" aria-label="清空当前内容" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg>
        </button>
      </nav>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            正在初始化搜索引擎
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    


<nav class="md-nav md-nav--primary" aria-label="导航栏" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="." title="GMLib 文档" class="md-nav__button md-logo" aria-label="GMLib 文档" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg>

    </a>
    GMLib 文档
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
    
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" data-md-toggle="toc" type="checkbox" id="__toc">
      
      
        
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          快速开始
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="." class="md-nav__link md-nav__link--active">
        快速开始
      </a>
      
        

<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    一、快速开始
  </a>
  
    <nav class="md-nav" aria-label="一、快速开始">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#11" class="md-nav__link">
    1.1 文件目录
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#12-cmake" class="md-nav__link">
    1.2 通过CMake编译（推荐）
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#gmlib_1" class="md-nav__link">
    二、GMLib 库使用
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    三、其它
  </a>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_2" type="checkbox" id="__nav_2" >
      
      
      
      
        <label class="md-nav__link" for="__nav_2">
          API文档
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="API文档" data-md-level="1">
        <label class="md-nav__title" for="__nav_2">
          <span class="md-nav__icon md-icon"></span>
          API文档
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="err/" class="md-nav__link">
        错误处理
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="utils/" class="md-nav__link">
        功能函数
      </a>
    </li>
  

            
          
            
              
  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_2_3" type="checkbox" id="__nav_2_3" >
      
      
      
      
        <label class="md-nav__link" for="__nav_2_3">
          大整数库
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="大整数库" data-md-level="2">
        <label class="md-nav__title" for="__nav_2_3">
          <span class="md-nav__icon md-icon"></span>
          大整数库
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="bint/" class="md-nav__link">
        基本运算
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="math/" class="md-nav__link">
        数学函数
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="ec/" class="md-nav__link">
        椭圆曲线
      </a>
    </li>
  

            
          
            
              
  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_2_5" type="checkbox" id="__nav_2_5" >
      
      
      
      
        <label class="md-nav__link" for="__nav_2_5">
          密码算法
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="密码算法" data-md-level="2">
        <label class="md-nav__title" for="__nav_2_5">
          <span class="md-nav__icon md-icon"></span>
          密码算法
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="cipher/" class="md-nav__link">
        分组密码
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="hash/" class="md-nav__link">
        哈希函数
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="publickey/" class="md-nav__link">
        公钥密码
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="stream/" class="md-nav__link">
        流密码
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="license/" class="md-nav__link">
        关于
      </a>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    一、快速开始
  </a>
  
    <nav class="md-nav" aria-label="一、快速开始">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#11" class="md-nav__link">
    1.1 文件目录
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#12-cmake" class="md-nav__link">
    1.2 通过CMake编译（推荐）
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#gmlib_1" class="md-nav__link">
    二、GMLib 库使用
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    三、其它
  </a>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          <div class="md-content" data-md-component="content">
            <article class="md-content__inner md-typeset">
              
                


<h1 id="gmlib">GMLib</h1>
<p>支持国密 SM4/AES-ECB/CBC/GCM，SM3，SM2签名/加密，ZUC算法 的密码库，文档页面<a href="https://oldprincess.github.io/gmlib/">GMLib Docs</a> ，项目地址 <a href="https://github.com/oldprincess/gmlib/">gmlib</a></p>
<p>开发这个库的时候参考了很多密码库，例如 <a href="https://github.com/weidai11/cryptopp">Crypto++</a>，<a href="https://github.com/openssl/openssl">openssl</a>，<a href="https://github.com/guanzhi/GmSSL">GmSSL</a>，<a href="https://www.oryx-embedded.com/doc/">Oryx Embedded</a> 以及 <code>JAVA JDK</code>，所以很多地方都能看到这些库的影子</p>
<h2 id="_1">一、快速开始</h2>
<h3 id="11">1.1 文件目录</h3>
<p>整个项目主要分为4部分，<code>include</code> 目录为 <code>gmlib</code> 库向外暴露的接口， <code>src</code> 目录为实现的源代码，<code>test</code> 目录为测试函数，<code>demo</code> 目录为一些调用样例</p>
<h3 id="12-cmake">1.2 通过CMake编译（推荐）</h3>
<p>测试环境1：<code>Windows 10</code> 系统，<code>VS</code> 版本2019，<code>CMake</code> 版本3.15</p>
<p>测试环境2：<code>Ubuntu 18.04.6 LTS</code> 系统，<code>gcc</code> 版本7.5.0，<code>CMake</code> 版本3.10.2</p>
<p>若 <code>Windows</code> 电脑中没有配置 <code>CMake</code> 环境，则可从 https://cmake.org/download/ 处下载安装配置</p>
<ul>
<li>拷贝 gmlib 项目至本机(也可以通过下载zip包的方式拷贝)</li>
</ul>
<div class="highlight"><pre><span></span><code>git clone https://github.com/oldprincess/gmlib.git
</code></pre></div>
<p>执行下述命令</p>
<div class="highlight"><pre><span></span><code><span class="c1"># 进入 gmlib 目录</span>
<span class="nb">cd</span> gmlib
<span class="c1"># 创建 build 目录，避免污染源代码</span>
mkdir build
<span class="nb">cd</span> build
</code></pre></div>
<ul>
<li>执行cmake，以Release模式编译</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="c1"># linux</span>
cmake .. -DCMAKE_BUILD_TYPE<span class="o">=</span>Release
cmake --build .
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="c1"># Windows(VS)</span>
cmake ..
cmake --build . --config release
</code></pre></div>
<p>若编译成功，则应该在<code>build</code>目录下生成结果文件，在其下的<code>bin</code>和<code>lib</code>目录为生成的测试文件与lib文件</p>
<ul>
<li>在控制台中执行 <code>bin</code> 目录下编译出的测试文件 <code>gmlib-test</code></li>
</ul>
<p>应当可在控制台中看到输出 <code>test finish!</code>，说明测试结果正确</p>
<p><strong>注</strong>：在 linux 系统中编译流程同上</p>
<h2 id="gmlib_1">二、GMLib 库使用</h2>
<p>以 1.2 中的 cmake 方式编译为例，编译完成后生成的库文件位于 <code>gmlib/build/lib</code> 目录，库的名称均为 <code>gmlib</code>。</p>
<ul>
<li>错误信息输出至 <code>stderr</code>，例如如下代码：</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;gmlib/bint.h&gt;</span><span class="cp"></span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">BINT</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w">  </span><span class="c1">// 声明大整数变量</span>
<span class="w">    </span><span class="c1">// 从字符串中以10进制读入大整数</span>
<span class="w">    </span><span class="n">bint_from_str</span><span class="p">(</span><span class="o">&amp;</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;a&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">);</span><span class="w"> </span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<ul>
<li>控制台中将提示</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="o">[</span>Error<span class="o">]</span> Bad string input, need <span class="o">[</span><span class="m">0</span>, <span class="m">9</span><span class="o">]</span>, get a. File <span class="sb">`</span>..<span class="se">\s</span>rc<span class="se">\b</span>int<span class="se">\b</span>int_cast.c<span class="sb">`</span>, line <span class="m">68</span>, <span class="k">in</span> <span class="sb">`</span>bint_from_str<span class="sb">`</span>
</code></pre></div>
<p>该错误提示是由 <code>GMLIB_DEBUG</code> 宏控制，若想关闭该提示，可修改 CMakeLists.txt 文件，将该行注释</p>
<div class="highlight"><pre><span></span><code>set<span class="o">(</span>CMAKE_C_FLAGS -DGMLIB_DEBUG<span class="o">)</span>
</code></pre></div>
<p>再重新编译工程</p>
<p>更多使用样例请参考docs文档和demo目录</p>
<h2 id="_2">三、其它</h2>
<ul>
<li>
<p><strong>为什么要写这个密码库</strong>：
想自己造个轮子，如果可以的话可以方便后人对密码学实现这块的学习。
我最初学习算法我是通过 <code>openssl</code> 库学习的，<code>openssl</code> 库代码风格对我而言过于晦涩复杂。之后找到了阅读起来更为易懂的 <code>oryx-embedded</code>（一个嵌入式的密码库）。在学习大整数运算时我是阅读 <code>Java JDK</code> 的源码学习的，虽然代码风格优秀，但 Java 中全部源码集中在一个文件中，函数调用关系不易捋清，而且英文文档和代码对我而言阅读起来远不及中文方便。</p>
</li>
<li>
<p><strong>后续这个库的开发方向</strong>：
后续主要随着自己的喜好开发，预计先将国密标准的算法全部写完，之后预计增加其它的工作模式和密码算法，再进行些效率上的优化。</p>
</li>
</ul>




              
            </article>
            
          </div>
        </div>
        
      </main>
      
        <footer class="md-footer">
  
    
    <nav class="md-footer__inner md-grid" aria-label="页脚" >
      
      
        
        <a href="err/" class="md-footer__link md-footer__link--next" aria-label="下一页: 错误处理" rel="next">
          <div class="md-footer__title">
            <div class="md-ellipsis">
              <span class="md-footer__direction">
                下一页
              </span>
              错误处理
            </div>
          </div>
          <div class="md-footer__button md-icon">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
          </div>
        </a>
      
    </nav>
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    <script id="__config" type="application/json">{"base": ".", "features": [], "search": "assets/javascripts/workers/search.ecf98df9.min.js", "translations": {"clipboard.copied": "\u5df2\u590d\u5236", "clipboard.copy": "\u590d\u5236", "search.config.lang": "ja", "search.config.pipeline": "trimmer, stemmer", "search.config.separator": "[\\s\\-\uff0c\u3002]+", "search.placeholder": "\u641c\u7d22", "search.result.more.one": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.more.other": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 # \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.none": "\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.one": "\u627e\u5230 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.other": "# \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.placeholder": "\u952e\u5165\u4ee5\u5f00\u59cb\u641c\u7d22", "search.result.term.missing": "\u7f3a\u5c11", "select.version.title": "\u9009\u62e9\u5f53\u524d\u7248\u672c"}}</script>
    
    
      <script src="assets/javascripts/bundle.9c69f0bc.min.js"></script>
      
    
  </body>
</html>